package com.kt.sso.test.exection;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.nio.file.AccessDeniedException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Desc:
 *
 * @author: keen
 * Date: 2020-06-26
 * Time: 21:36
 */
@ControllerAdvice
@Slf4j
public class ExceptionCatch {

    private final static Map<Class<? extends Exception>, String> EXCEPTION_CODE = new HashMap<>();

    //private static ImmutableMap<Class<? extends Throwable>,String> EXCEPTIONS;

    static {
        EXCEPTION_CODE.put(AccessDeniedException.class, "权限不足,禁止访问!");
    }

    @ExceptionHandler(Exception.class)
    public String handler(Exception e) {
        String message = EXCEPTION_CODE.get(e);
        if (StringUtils.isBlank(message)) {
            message = "系统异常";
        }
        log.error("出现了异常", e);
        return message;
    }
}
